package solutions.leetcode.easy;

import solutions.lib.BaseSolution;

import java.util.*;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/contains-duplicate-ii/">219. 存在重复元素 II</a>
 */
public class Solution219 extends BaseSolution {

    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Queue<Integer> queue = new LinkedList<>();
        Set<Integer> set = new HashSet<>();
        int p = 0;
        while (p <= k && p < nums.length) {
            queue.offer(nums[p]);
            if (!set.add(nums[p])) {
                return true;
            }
            p++;
        }
        while (p < nums.length) {
            set.remove(queue.poll());
            queue.offer(nums[p]);
            if (!set.add(nums[p])) {
                return true;
            }
            p++;
        }
        return false;
    }

    public static void main(String[] args) {
        Solution219 solution = new Solution219();
        System.out.println(solution.containsNearbyDuplicate(new int[]
                        {1, 2, 3, 1, 2, 3}
                , 3));
    }
}